package com.qf.test.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.test.pojo.DtsBrand;
import com.qf.test.service.BrandService;
import com.qf.test.util.ResponseUtil;
import com.qf.test.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//admin/brand/list?page=1&limit=20&id=aa&name=ss&sort=add_time&order=desc
//把这个类交给spring管理

/**
 * 品牌制作商
 */
@RestController
//解决前后端跨域问题
@CrossOrigin("*")
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;


    /**
     * 品牌制造商首页的查询
     *
     * @param page
     * @param limit
     * @param id
     * @param name
     * @param sort
     * @param order
     * @return
     */
    @GetMapping("/list")
    public Object list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer limit, String id, String name,
            @RequestParam(defaultValue = "add_time") String sort,
            @RequestParam(defaultValue = "desc") String order) {
        //查询并返回分页对象
        IPage<DtsBrand> iPage = brandService.querbrand(page, limit, id, name, sort, order);
        //获取结果级
        List<DtsBrand> records = iPage.getRecords();
        //获取总条数
        long count = iPage.getTotal();

        //封装对象并返回
        HashMap<String, Object> data = new HashMap<>();
        data.put("total", count);
        data.put("items", records);
        return ResponseUtil.ok(data);
    }

    /**
     * http://localhost:8083/admin/brand/create
     */
    @PostMapping("/create")
    public Object addcreate(@RequestBody DtsBrand dtsBrand) {

        DtsBrand data = brandService.add(dtsBrand);
        return ResponseUtil.ok(data);

    }

    @PostMapping("/update")
    public Object update(@RequestBody DtsBrand dtsBrand) {
        int data = brandService.update(dtsBrand);
        return ResponseUtil.ok(data);
    }


    @PostMapping("/delete")
    public Object delete(@RequestBody DtsBrand dtsBrand) {
        int data = brandService.update(dtsBrand);
        return ResponseUtil.ok(data);
    }


    @RequestMapping("/catAndAdmin")
    public Object catAndAdmin() {
        Map<String, Object> data = new HashMap<>();
        //返回一个管理员权限
        List<CatVo> list = brandService.findall();
        //返回一级目录
        List<CatVo> categoryList = brandService.all1();

        List<CatVo> list2 = brandService.all2();

        for (int i = 0; i < categoryList.size() ; i++) {
            categoryList.get(i).children = new ArrayList<>();

            for (int j = 0; j <list2.size() ; j++) {
                if(categoryList.get(i).getValue().equals(list2.get(j).getPid())){
                    categoryList.get(i).children.add(list2.get(j));
                }
            }

            }

        data.put("adminList", list);
        data.put("categoryList", categoryList);
        return ResponseUtil.ok(data);
    }


}
